package com.itheima.dao.impl;

import com.itheima.dao.IStudentDao;
import com.itheima.domain.Student;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {

    private static  ArrayList<Student> list= new ArrayList<>();
    static {
        Student student1 = new Student("00001", "鹿晗", 30, "1991");
        Student student2 = new Student("00002", "吴签", 31, "1990");
        Student student3 = new Student("00003", "坤坤", 26, "1995");
        Student student4 = new Student("00004", "滔滔", 28, "1993");
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
    }
    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        list.remove(index);
    }

    @Override
    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
        list.set(index,newStu);
    }

    @Override
    public ArrayList<Student> findAllStudents() {
        return list;
    }

    @Override
    public int getIndex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }
}
